package dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author Yenchingcheng
 * @Date 2023/6/13 20:33
 * @Description
 **/
public class JDKDynamicProxy implements InvocationHandler {

    private Object target;

    //构造器，对target进行初始化
    public JDKDynamicProxy(Object target) {
        this.target = target;
    }

    //重写invoke方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        if(name.equals("sleep")){
            System.out.println("good night.");
        }else if(name.equals("wakeUp")){
            System.out.println("good morning.");
        }
        return method.invoke(target,args);
    }
}
